package com.cicdi.util;

import com.platon.tx.gas.GasProvider;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.math.MathContext;

/**
 * 转换器
 *
 * @author haypo
 * @date 2021/1/4
 */
public class ConvertUtil {
    /**
     * 将hrp单位值转换为von单位的值
     *
     * @param hrpValue hrp值
     * @return von单位的值
     */
    public static BigInteger hrp2Von(BigDecimal hrpValue) {
        return hrpValue.multiply(BigDecimal.TEN.pow(18)).toBigInteger();
    }

    /**
     * 将hrp单位值转换为von单位的值
     *
     * @param hrpValue hrp值
     * @return von单位的值
     */
    public static BigInteger hrp2Von(String hrpValue) {
        return new BigDecimal(hrpValue).multiply(BigDecimal.TEN.pow(18)).toBigInteger();
    }

    /**
     * 计算交易手续费
     *
     * @param gas      gas用量
     * @param gasPrice gas价格
     * @return 交易手续费
     */
    public static BigDecimal getTxFee(BigInteger gas, BigInteger gasPrice) {
        return von2Hrp(gasPrice.multiply(gas));
    }

    public static GasProvider genGasProvider(BigInteger gasLimit, BigInteger gasPrice) {
        return new GasProvider() {
            @Override
            public BigInteger getGasLimit() {
                return gasLimit;
            }

            @Override
            public BigInteger getGasPrice() {
                return gasPrice;
            }
        };
    }

    public static BigDecimal von2Hrp(BigInteger vonValue) {
        return new BigDecimal(vonValue).divide(BigDecimal.TEN.pow(18), MathContext.DECIMAL128);
    }
}
